var pageii = null;
var grid = null;
$(function() {
	
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "user_id",
			name : "用户id",
			hide : true,
		}, {
			colkey : "user_name",
			name : "用户名",
			isSort:true,
		},{
			colkey : "user_account",
			name : "账号",
			isSort:true,
			
		}, {
			colkey : "role_name",
			name : "所属角色"
		},{
			colkey : "state",
			name : "账号状态",
			width : '90px',
			isSort:true,
			renderData:function( rowindex ,data, rowdata, colkey){
				if( rowdata["state"]=='1'){
					return "离职";
				}else if(rowdata["state"]=='0'){
					return "在职";
		}}}, {
			colkey : "create_time",
			name : "时间",
			isSort:true,
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd hh:mm:ss");
			}
		} ],
		jsonUrl : rootPath + '/system/userInfo/findByPage.shtml',
		checkbox : true,
		serNumber : true,
		checkValue : 'user_id' 
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addAccount").click("click", function() {
		addAccount();
	});
	$("#editAccount").click("click", function() {
		editAccount();
	});
	$("#delAccount").click("click", function() {
		delAccount();
	});
	$("#permissions").click("click", function() {
		permissions();
	});
	$("#role").click("click", function() {
		role();
	});
});
function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		  Wind.use("artDialog", "iframeTools", function () {
			  art.dialog('只能选中一个').time(2);
		  });
		return;
	}
	showModal("user_info","编辑", rootPath + "/system/userInfo/editUI.shtml?userId="+cbox,680,450);

}
function addAccount() {
	var deptId =$("#user_dept_id").val();
	var deptName =$("#dept_name").val();
	console.log(deptId+deptName)
	if (deptId == " " || deptId == null) {
		  Wind.use("artDialog", "iframeTools", function () {
			  art.dialog.tips('请选择部门').time(2);
		  });

		return;
	}
	showModal("user_info","新增", rootPath + "/system/userInfo/addUI.shtml?deptId="+deptId+"&deptName="+deptName,680,450);
}
function delAccount() {
		var cbox = grid.getSelectedCheckbox();
		if (cbox == "") {
			tips("请选择需要删除的项",1);
			return;
		}
		confirm('你确定要删除吗？', function() {
			var url = rootPath + '/system/userInfo/deleteEntity.shtml';
			var s = CommnUtil.ajax(url, {
				ids : cbox.join(",")
			}, "json");
			if(s=="success"){
				tips("删除成功",1);
				loadData();
			}else{
				tips("删除失败",1);
			}
		});

	};
	
function loadData(){
	var searchParams = $("#searchForm").serializeJson();// 初始化传参数
	grid.setOptions({
		data : searchParams
	});
}
function role() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("请选择一个对象！");
		return;
	}
	
	var url = rootPath + '/system/roleInfo/selRole.shtml?sysRoleFormMap.user_id='+cbox;
	pageii = layer.open({
		title : "分配角色",
		type : 2,
		area : [ "550px", "80%" ],
		content : url
	});
}

function tips(title,time){
	time = time?time:3;
	 Wind.use("artDialog", "iframeTools", function () {
	        art.dialog.tips(title).time(time);
	 });
}


function confirm(title,yes,no){
	 Wind.use("artDialog", "iframeTools", function () {
		art.dialog.confirm(title, function () {
			if(yes){
				yes()
			}
		}, function () {
			if(no){
				no()
			}
		});
	 });
}


function showModal(id,title, url,width,height) {
    Wind.use("artDialog", "iframeTools", function () {
        art.dialog.open(url, {
            title: title,
            id: id,
            width: width,
            height: height,
            lock: true,
            fixed: true,
            background: "#CCCCCC",
            opacity: 0.7
        });
    });
}